import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 14-8-26
 * Time: 下午10:42
 * To change this template use File | Settings | File Templates.
 */
public class ThreadExample {

    final BlockingQueue<Object> blockingQueue = new ArrayBlockingQueue<Object>(10);
    Thread thread = new Thread("consumer thread") {

        public void run() {

            for (; ;) {

                try {
                    //Object o = blockingQueue.take();
                    Object o = blockingQueue.poll(1, TimeUnit.SECONDS);
                    if (o == null) {


                    }
                } catch (InterruptedException e) {
                    break;
                } catch (Exception e) {

                    //handle exception

                }

            }

        }

    };


    private class BlockingQ {


        private Object o1 = new Object();
        private Object o2 = new Object();
        private Queue<Object> linkedList = new LinkedList<Object>();
        private int maxLength = 10;

        public Object take() {

            synchronized (o1) {

                if (linkedList.size() == 0) {

                    try {
                        o1.wait();
                    } catch (InterruptedException e) {

                    }

                }
                synchronized (o2) {

                    if (linkedList.size() == maxLength) {

                        o2.notifyAll();

                    }
                    return linkedList.poll();

                }


            }


        }

        public void offer(Object object) {

            synchronized (o1) {

                if (linkedList.size() == 0) {

                    o1.notifyAll();
                }
                synchronized (o2) {

                    if (linkedList.size() == maxLength) {

                        try {
                            o2.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                        }

                    }

                    linkedList.add(object);

                }

            }

        }

    }

    private class BlockingLock {

        private Lock lock = new ReentrantLock();
        private Condition notEmpty = lock.newCondition();
        private Condition notFull = lock.newCondition();
        private Queue<Object> linkedList = new LinkedList<Object>();
        private int maxLength = 10;

        public Object take() throws Exception {

            lock.lock();

            try {

                if (linkedList.size() == 0) {

                    notEmpty.await();

                }

                if (linkedList.size() == maxLength) {

                    notFull.signalAll();

                }
                return linkedList.poll();
            } finally {


                lock.unlock();


            }

        }

        public void offer(Object object) throws Exception {


            lock.lock();

            try {

                if (linkedList.size() == 0) {

                    notEmpty.signalAll();

                }
                if (linkedList.size() == maxLength) {


                    notFull.await();

                }

            } finally {
                lock.unlock();
            }


        }


    }

    private class Counter {

    private AtomicInteger max = new AtomicInteger(0);

    public void set(int value) {

        for (; ;) {

            int current = max.get();
            if (value > current) {

                if (max.compareAndSet(current, value)) {

                    break;

                } else {

                    continue;

                }

            } else {

                break;

            }

        }

    }
}

    private class BeanManager{

        private Map<String,Object>  map = new HashMap<String,Object>();

        public Object getBean(String key){

            synchronized (map){

                Object bean = map.get(key);
                

            }

        }

    }
}
